<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
class Class_M_COST_TVSP extends Class_M_DAO {
	//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table  = "M_COST_TVSP";
	
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey   = "id";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = "matrix_cd";
	 
	 protected $_cols  = array(
		"matrix_cd"        => array(AOKI_VARTYPE_INT),
		"electric_id"      => array(AOKI_VARTYPE_STR),
		"tele_date_start"  => array(AOKI_VARTYPE_DATE),
		"tele_date_end"    => array(AOKI_VARTYPE_DATE),
		"mater_id"         => array(AOKI_VARTYPE_INT),
		"grp"              => array(AOKI_VARTYPE_STR),
		"cosrt"            => array(AOKI_VARTYPE_STR)		
	 );
	 
	 protected $_checks  = array(
		"electric_id"     => array( "EXIST" ),
		"matrix_cd"       => array( "EXIST" ),
		"mater_id"        => array( "EXIST", "NUM" ),
		"tele_date_start" => array( "EXIST"),
		"tele_date_end"   => array( "EXIST"),
		"grp"             => array( "EXIST", "NUM" ),
		"cosrt"           => array( "EXIST", "NUM" )		
	 );
	 
	 //maxlength 
	 protected $_maxlens  = array(
		"electric_name "    => 50 ,		
	 );
	 
	 public function search($where ,&$page = NULL ){
		$sql = "SELECT a.*,c.mater_name,c.cm_memo,b.week_cd,b.week_seq,b.flier_date,(select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name from {$this->_table}  a" .
				" inner join  M_MATRIX_TERM b on a.matrix_cd = b.matrix_cd " .
				" inner join  M_COST_TVSP_MATER c on a.mater_id = c.id ";
	
		$sqlcount = " SELECT count(*)  as cnt from {$this->_table}  a  inner join  M_MATRIX_TERM b on a.matrix_cd = b.matrix_cd inner join  M_COST_TVSP_MATER c on a.mater_id = c.id  ";
		$orderby = " a.matrix_cd,a.id";
		return  parent::search($where, $page, $sql, $sqlcount,$orderby);
	}
	
	 public function searchcsv($where  ){
	 	$page = NULL;	
	 		$sql = "SELECT a.*,c.mater_name,c.cm_memo,b.week_cd,b.week_seq,b.flier_date,d.electric_name from {$this->_table}  a" .
				" inner join  M_MATRIX_TERM b on a.matrix_cd = b.matrix_cd " .
				" inner join  M_COST_TVSP_MATER c on a.mater_id = c.id " .
				" inner join  M_ELECTRIC d on a.electric_id = d.id ";
			$page = NULL ;
			$orderby = " a.matrix_cd,a.id";
			return  parent::search($where, $page, $sql, NULL,$orderby);
	 }
	 
	public function searchcostcsv($begin_date, $end_date ,$electric_id =null){
 		$page = NULL;	
 		$sql = " SELECT a.*,c.mater_name,c.cm_memo,b.week_cd,b.week_seq,b.flier_date,d.electric_name,suma.now_grp,suma.now_cost, 10 as pre_grp,10 as pre_cost  from {$this->_table}  a" .
			" inner join  M_MATRIX_TERM b on a.matrix_cd = b.matrix_cd and b.flier_date >= {$begin_date} and  b.flier_date <= {$end_date}" .
			" inner join  M_COST_TVSP_MATER c on a.mater_id = c.id " .
			" inner join  M_ELECTRIC d on a.electric_id = d.id " .
			" inner join  (select aa.matrix_cd,sum(grp) as now_grp, sum(cosrt) as now_cost from  {$this->_table}  aa  " .
			" inner join  M_MATRIX_TERM bb on aa.matrix_cd = bb.matrix_cd  " .
			" where  bb.flier_date >= {$begin_date} and  bb.flier_date <= {$end_date} " .
			" group by aa.matrix_cd ) suma on suma.matrix_cd = a.matrix_cd ";
			
		if (!empty($electric_id)) {
			$sql = $sql." where a.electric_id = {$electric_id}";
		}
		$page = NULL ;
		$orderby = " a.matrix_cd,a.id";
		return  parent::search(NULL, $page, $sql, NULL,$orderby);
	 }
	 
	public function exportcsv( $where ){
	 	$page = NULL;	
	 		$sql = "SELECT a.*,c.mater_name,c.cm_memo,b.week_cd,b.week_seq,b.flier_date,d.electric_name from {$this->_table}  a" .
				" inner join  M_MATRIX_TERM b on a.matrix_cd = b.matrix_cd " .
				" inner join  M_COST_TVSP_MATER c on a.mater_id = c.id " .
				" inner join  M_ELECTRIC d on a.electric_id = d.id ";
			$page = NULL ;
			$orderby = " a.matrix_cd,a.id";
			return  parent::search($where, $page, $sql, NULL,$orderby);
	 }
	 
	public function get_matrix_cd($intdate){	
		$db = new Class_DB;
		$sql = " select min(a.matrix_cd)   as matrix_cd from M_MATRIX_TERM a where a.week_date_begin <= {$intdate} AND week_date_end >= {$intdate}  ";
		
		if ( !($db->query( $sql)) ) {
			return 0;
		}
		
		if ( $row = $db->fetch() ){
			return $row["matrix_cd"] ;
		}
		
		return 0;
	} 
}

?>